var app = getApp(),
  a = app.requirejs("core"),
  b = app.requirejs("api/kjb");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    countDownDay: 0,
    countDownHour: 0,
    countDownMinute: 0,
    countDownSecond: 0,
    list: [],
    show_time: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    a.setting();
    this.setData(options)
    this.detail()
    this.goodslist()
  },
  /**
   * 商品详情
   */
  detail: function () {
    var that = this,
      id = that.data.id;
    b.kj_detail(id, that, function (t) {
      that.setData(t)
      //倒计时
      if (t.bargain_info.end_time) {
        var time = t.bargain_info.end_time;
        wx.setNavigationBarTitle({
          title: t.bargain_info.bargain_name || "活动详情"
        })
        b.Countdown(time, function (i) {
          that.setData(i)
        })
      }
    })
  },
  goodslist: function () {
    var that = this;
    b.kj_list('', 1, 1, that, function (t) {
      that.setData(t)
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({ list: [] });
    this.detail();
    this.goodslist();
    wx.stopPullDownRefresh();
  },
  /**
   * 跳转至地图
   */
  navigate: function () {
    var that = this;
    var t = this.data.about_info;
    if (t.name && t.lat && t.lng) {
      a.tx_map(t.lat, t.lng, t.name)
    } else {
      a.toast('获取位置失败')
    }
  },
  /**
 * 跳转至详情
 */
  url: function (e) {
    var data = a.pdata(e);
    wx.navigateTo({
      url: '/yb_shop/pages/kanjia/goods_info/index?id=' + data.id
    })
  },
  /**
   * 直接购
   */
  shoping: function (e) {
    var that = this,
      data = a.pdata(e);
    if (!that.data.show_time) {
      a.alert('该活动已经结束')
      return false;
    }
    if (that.data.bargain_info.bargain_inventory < 1) {
      a.alert('库存不足')
      return false;
    }
    wx.navigateTo({
      url: "/yb_shop/pages/kanjia/order/create/index?bargain_id=" + that.data.bargain_info.id + "&total=1&uid=" + getApp().getCache("userinfo").uid + "&activity_order_type=0&current_price=" + that.data.bargain_info.original_price
    })
  },
  /**
   * 发起砍
   */
  formSubmit: function (e) {
    var that = this,
      form_id = e.detail.formId,
      bargain_id = e.detail.value.id,
      data = a.pdata(e);
    if (!that.data.show_time) {
      a.alert('该活动已经结束')
      return false;
    }
    if (that.data.bargain_info.bargain_inventory < 1) {
      a.alert('库存不足')
      return false;
    }
   
      wx.navigateTo({
        url: '/yb_shop/pages/kanjia/discount_info/index?id=' + bargain_id + '&form_id=' + form_id
      })
 
  },
  /**
   * 打电话
   */
  phone: function (e) {
    a.phone(e)
  }

})